//
//  MSDayStatisticsController.m
//  觅尚
//
//  Created by No.8 on 2019/3/26.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSYearCountController.h"
#import "MSDayCountCell.h"
#import "MSDayProgressView.h"
#import "CircleProgressView.h"
#import "MSDayRoomModel.h"
#import "PickerView.h"
#define pageSize 20


@interface MSYearCountController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger page;
    //    NSInteger pageSize;
    NSInteger count;
    
}

/** tableView*/
@property(nonatomic,strong)UITableView *tableView;

@property (nonatomic, strong) CircleProgressView *circleProgress;
@property (nonatomic, strong) NSTimer *timer;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *DataArray;

/** <#name#>*/
@property(nonatomic,strong)NSString * TimeStr;

/** <#name#>*/
@property(nonatomic,strong)UILabel * timeLabel;

@end

@implementation MSYearCountController
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    
    [self.tableView.mj_header beginRefreshing];
    
    
    
    
    
}
- (void)MJChiBaoZiHeaderRefresh{
    //    [self.tableView.header beginRefreshing];
    //    [self loadShowproduct];
    page = 1;
    [self ReloadData];
    
    
}
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    
    [self FootderRefreshReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.DataArray = [NSMutableArray new];
    self.view.backgroundColor = backgroundcolor;
    
    [self CreatView];
    
    [self tableView];
    
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
    
}

- (void)FootderRefreshReloadData{
    [MBManager showLoading];
    [self.DataArray removeAllObjects];
     NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"queryTime":@"year",@"page":@(page),@"pageSize":@(pageSize),@"Time":self.TimeStr};
    [HTTPRequest POST:MSgetRoomIncome params:params succeed:^(id responseObject) {
        //        WYCLog(@"=============%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            //            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            self.DataArray = [MSDayRoomModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
        }else{
            [MBManager hideAlert];
            //            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page--;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        //        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
    
}
- (void)ReloadData{
    [MBManager showLoading];
    [self.DataArray removeAllObjects];
    
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"queryTime":@"year",@"page":@(page),@"pageSize":@(pageSize),@"Time":self.TimeStr};
        WYCLog(@"=============%@",params);
    [HTTPRequest POST:MSgetRoomIncome params:params succeed:^(id responseObject) {
        //        WYCLog(@"=============%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            count = [responseObject[@"data"][@"count"] integerValue];
            NSMutableArray *temporaryArray = [MSDayRoomModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            [temporaryArray addObjectsFromArray:self.DataArray];
            self.DataArray = temporaryArray;
            
            
        }else{
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            //             [self.tableView.mj_footer endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [self.tableView.mj_header endRefreshing];
        //         [self.tableView.mj_footer endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}

- (void)CreatView{
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 320 * px)];
    HeaderView.backgroundColor = backgroundcolor;
    [self.view addSubview:HeaderView];
    
    UIView * timeView = [[UIView alloc] initWithFrame:CGRectMake(50 * px, 50 * px, ScreenWidth * 0.5, 120 * px)];
    timeView.layer.cornerRadius = 10 * px;
    timeView.layer.borderWidth = 1;
    timeView.layer.borderColor = WYCRGBColor(33, 46, 45).CGColor;
    [HeaderView addSubview:timeView];
    
    
    /** 日历图标*/
    UIButton * TimeButton = [[UIButton alloc] initWithFrame:CGRectMake(30 * px, 30 * px, 60 * px, 60 * px)];
    //    [TimeButton setImage:[UIImage imageNamed:@"calendar icon"] forState:UIControlStateNormal];
    [TimeButton setBackgroundImage:[UIImage imageNamed:@"calendar icon"] forState:UIControlStateNormal];
    [TimeButton addTarget:self action:@selector(TimeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    TimeButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    //    TimeButton.backgroundColor = [UIColor redColor];
    [timeView addSubview:TimeButton];
    
    /** 左按钮*/
    UIButton *  leftButton= [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(TimeButton.frame) + 40 * px, TimeButton.y, 40 * px, TimeButton.height)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"日期选择-左箭头"] forState:UIControlStateNormal];
//    [leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [timeView addSubview:leftButton];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    self.TimeStr = [NSString stringWithFormat:@"%@-01,%@-12",currentTimeString,currentTimeString];
    /** 时间*/
    UILabel * timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftButton.frame) + 40 * px, TimeButton.y, timeView.width / 2.4, TimeButton.height)];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.text = [NSString stringWithFormat:@"%@",currentTimeString];
    timeLabel.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
    [timeView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    /** 右按钮*/
    UIButton *  rightButton= [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timeLabel.frame) + 40 * px, TimeButton.y, 40 * px, TimeButton.height)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"日期选择-右箭头"] forState:UIControlStateNormal];
//    [rightButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [timeView addSubview:rightButton];
    
    
    
    //    UITextField * seachField = [[UITextField alloc] initWithFrame:CGRectMake(timeView.x, CGRectGetMaxY(timeView.frame) + 30 * px, HeaderView.width - timeView.x * 2, 120 * px)];
    //    seachField.placeholder = @"搜索房间号";
    //    seachField.layer.borderColor = [UIColor grayColor].CGColor;
    //    seachField.layer.borderWidth = 1.0f;
    //    seachField.layer.cornerRadius = 10 * px;
    //    seachField.keyboardType = UIKeyboardTypeNumberPad;
    //    [seachField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    //    [seachField setValue:[UIFont systemFontOfSize:SYRealValue(35 / 2)] forKeyPath:@"_placeholderLabel.font"];
    //    seachField.textColor = [UIColor whiteColor];
    //    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    //    imgView.frame = CGRectMake(20 * px, 30 * px, 60 * px, 60 * px);
    //    seachField.leftView = imgView;
    //    seachField.leftViewMode = UITextFieldViewModeAlways;
    //    [HeaderView addSubview:seachField];
    
    
    
    
    
    UIView * linView = [[UIView alloc] initWithFrame:CGRectMake(timeView.x, CGRectGetMaxY(timeView.frame) + 30 * px, HeaderView.width - timeView.x * 2, 120 * px)];
    linView.backgroundColor = BackViewColor;
    [HeaderView addSubview:linView];
    
    
    CGFloat titlelabelY = 20 * px;
    CGFloat titlelabelH = 80 * px;
    CGFloat titlelabelW = linView.width / 3;
    NSArray * titleArray = @[@"房间编号",@"开房次数",@"消费额"];
    for (NSInteger i = 0; i < titleArray.count; i ++) {
        /** <#name#>*/
        UILabel * titlelabel = [[UILabel alloc] init];
        titlelabel.x = titlelabelW * i;
        titlelabel.y = titlelabelY;
        titlelabel.width = titlelabelW;
        titlelabel.height = titlelabelH;
        titlelabel.textColor = [UIColor whiteColor];
        titlelabel.text = titleArray[i];
        titlelabel.textAlignment = NSTextAlignmentCenter;
        titlelabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [linView addSubview:titlelabel];
    }
    self.tableView.tableHeaderView = HeaderView;
    
    
}
- (void)TimeButtonClick{
    
    [PickerView showPickerView:[UIApplication sharedApplication].keyWindow componentNum:1 typePickerID:1 selectStr:nil StrBlock:^(NSString *string) {
//        NSLog(@"%@",string);
        self.TimeStr = [NSString stringWithFormat:@"%@-01,%@-12",string,string];
        self.timeLabel.text = [NSString stringWithFormat:@"%@",string];
        
        [self ReloadData];
    }];
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 320 * px) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.DataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSDayCountCell * cell = [MSDayCountCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.DayRoomModel = self.DataArray[indexPath.row];
    
    return cell;
}
@end
